<template>
    <div>
        <el-table :data="store.getters['user/getUserList']" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180" />
            <el-table-column prop="username" label="用户名称" width="180" />
            <el-table-column prop="rolename" label="所属角色"></el-table-column>
            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination class="page" :page-size="store.getters['user/getSize']" background layout="prev, pager, next"
            :total="store.getters['user/getCount']" @current-change="changePage" />
    </div>
</template>
<script lang='ts' setup>
import { useStore } from 'vuex';
import { userdelete } from '@/request/api';
import { ElMessageBox, ElMessage } from 'element-plus'
import { defineEmits } from 'vue';
// 调用仓库方法
let store = useStore();


// 调用数据总条数行动
store.dispatch('user/getCountActions')
// 调用管理员列表行动
store.dispatch('user/getUserListActions')

// 调用子传父方法
let emit = defineEmits(['edit']);

// 编辑 弹出弹窗  
const edit = (uid: number) => {
    // 通知父元素
    emit('edit',uid);
}

// 删除
const del = (uid: number) => {
    ElMessageBox.confirm(
        '确定要删除此数据吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            userdelete({ uid }).then(res => {
                if (res.code == 200) {
                    // 删除成功
                    ElMessage.success(res.msg);
                    // 重新渲染视图
                    store.dispatch('user/getUserListActions');
                    // 重新获取总条数
                    store.dispatch('user/getCountActions')

                } else {
                    // 删除成功
                    ElMessage.error(res.msg);
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消删除',
            })
        })

}



// 改变页码
const changePage = (num: number) => {
    console.log(num);
    store.dispatch('user/change_size', num)
}
</script>
<style scoped lang='less'>
.page {
    margin-top: 20px;
    float: right;
}
</style>